package cn.funeralobjects.minecraft.handler;

import cn.funeralobjects.minecraft.model.LoginResult;
import cn.funeralobjects.minecraft.model.Player;
import com.mojang.authlib.GameProfile;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;

import java.net.SocketAddress;

/**
 * 实现的简易检查基类
 *
 * @author FuneralObjects
 * Create date: 2020/11/23 2:31 AM
 */
public abstract class BasePlayerLoginHandler implements PlayerLoginHandler {

    @Override
    public Text checkCanLogin(SocketAddress socketAddress, GameProfile profile) {
        return toSimpleText(this.checkCanLogin(toPlayer(profile)));
    }

    /**
     * 自定义的检查登陆接口
     *
     * @param player 玩家信息
     * @return 登陆结果
     */
    public abstract LoginResult checkCanLogin(Player player);

    protected static Player toPlayer(GameProfile profile) {
        return new Player().setName(profile.getName())
                .setUuid(profile.getId().toString());
    }

    protected static Text toSimpleText(LoginResult loginResult) {
        return loginResult.getSuccess() ? null : new LiteralText(loginResult.getFailedMessage());
    }
}
